﻿using System.Web;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using NPOI.HPSF;
using Simple.Services;
using Simple.Services.System.ClassRoom;
using static Simple.Services.ClassDynamicCreateOrUpdateModel;

namespace Simple.WebApi.Controllers.System;

/// <summary>
/// 员工考勤规则管理
/// </summary>
[Route("api/StaffAttendance/[action]")]
[ApiController]
[Authorize]
public class StaffAttendanceController : ControllerBase
{
    private readonly StaffAttendanceRuleService _staffAttendanceRuleService;

    public StaffAttendanceController(StaffAttendanceRuleService staffAttendanceRuleService)
    {
        _staffAttendanceRuleService = staffAttendanceRuleService;
    }

    /// <summary>
    /// 查询员工考勤规则内容详情
    /// </summary>
    /// <param name="Id"></param>
    /// <returns></returns>
    [HttpGet]
    public async Task<AppResult> GetStaffAttendanceRuleInfo([FromQuery] Guid? Id)
    {
        var data = await _staffAttendanceRuleService.GetAsync(Id);
        return AppResult.Status200OK(data: data);
    }

    /// <summary>
    /// 员工考勤规则列表分页查询(PC端管理后台查询)
    /// </summary>
    /// <param name="model"></param>
    /// <returns></returns>
    [HttpGet]
    public async Task<AppResult> Page([FromQuery] StaffAttendanceRuleQueryPageInputModel model)
    {
        var data = await _staffAttendanceRuleService.GetPageAsync(model);
        return AppResult.Status200OK(data: data);
    }

    /// <summary>
    /// 新建员工考勤规则
    /// </summary>
    /// <param name="model"></param>
    /// <returns></returns>
    [HttpPost]
    public async Task<AppResult> Add([FromBody] StaffAttendanceRuleOutputModel model)
    {
        await _staffAttendanceRuleService.AddAsync(model);
        return AppResult.Status200OK("新增成功");
    }

    /// <summary>
    /// 编辑员工考勤规则
    /// </summary>
    /// <param name="model"></param>
    /// <returns></returns>
    [HttpPost]
    public async Task<AppResult> Edit([FromBody] StaffAttendanceRuleOutputModel model)
    {
        await _staffAttendanceRuleService.UpdateAsync(model);
        return AppResult.Status200OK("更新成功");
    }

    /// <summary>
    /// 删除员工考勤规则
    /// </summary>
    /// <param name="IdList"></param>
    /// <returns></returns>
    [HttpPost]
    public async Task<AppResult> Delete([FromBody] List<Guid?> IdList)
    {
        await _staffAttendanceRuleService.DeleteAsync(IdList);
        return AppResult.Status200OK("删除成功");
    }

    /// <summary>
    /// 员工考勤记录
    /// </summary>
    /// <param name="query"></param>
    /// <returns></returns>
    [HttpPost]
    public async Task<AppResult> GetStaffAttendanceRecord([FromBody] StaffAttendanceStatisticsReportInputModel query)
    {
        var data = await _staffAttendanceRuleService.GetStaffAttendanceRecord(query);
        return AppResult.Status200OK(data: data);
    }

    /// <summary>
    /// 查询员工某月考勤打卡记录
    /// </summary>
    /// <param name="query"></param>
    /// <returns></returns>
    [HttpPost]
    public async Task<AppResult> GetStaffAttendanceRecordByUserId([FromBody] StaffAttendanceRecordByUserIdInputModel query)
    {
        var data = await _staffAttendanceRuleService.GetStaffAttendanceRecordByUserId(query);
        return AppResult.Status200OK(data: data);
    }

    /// <summary>
    /// 查询员工某月考勤打卡记录(移动端)
    /// </summary>
    /// <param name="query"></param>
    /// <returns></returns>
    [HttpPost]
    public async Task<AppResult> GetMoveStaffAttendanceRecordByUserId([FromBody] StaffAttendanceRecordByUserIdInputModel query)
    {
        var data = await _staffAttendanceRuleService.GetMoveStaffAttendanceRecordByUserId(query);
        return AppResult.Status200OK(data: data);
    }

    /// <summary>
    /// 查询当日考勤打卡统计(移动端)
    /// </summary>
    /// <param name="query"></param>
    /// <returns></returns>
    [HttpPost]
    public async Task<AppResult> GetMoveStaffAttendanceRecordStatistics([FromBody] MoveStaffAttendanceRecordInputModel query)
    {
        var data = await _staffAttendanceRuleService.GetStaffAttendanceRecordStatistics(query);
        return AppResult.Status200OK(data: data);
    }

    /// <summary>
    /// 小程序首页当日考勤打卡统计(移动端)
    /// </summary>
    /// <returns></returns>
    [HttpGet]
    public async Task<AppResult> GetMoveStaffAttendanceRecordCurrentDayStatistics()
    {
        var data = await _staffAttendanceRuleService.GetStaffAttendanceRecordCurrentDayStatistics();
        return AppResult.Status200OK(data: data);
    }


    /// <summary>
    /// 老师考勤导出
    /// </summary>
    /// <param name="model"></param>
    /// <returns></returns>
    [HttpPost] 
    public async Task<FileContentResult> AttendanceRecordExcelModel(int month)
    {
        var file = await _staffAttendanceRuleService.AttendanceRecordExcelModel(month);
        return File(file, "application/octet-stream", fileDownloadName: $"老师{month}月考勤统计.xlsx");
    } 

    /// <summary>
    /// 学生考勤导出
    /// </summary>
    /// <param name="model"></param>
    /// <returns></returns>
    [HttpPost]
    public async Task<FileContentResult> AttendanceChildrensRecordExcelModel(int month)
    {
        var file = await _staffAttendanceRuleService.AttendanceChildrensRecordExcelModel(month);
        return File(file, "application/octet-stream", fileDownloadName: $"学生{month}月考勤统计.xlsx");
    }
}
